package com.qfedu.mall.api.controller;

import com.baomidou.kaptcha.Kaptcha;
import com.qfedu.mall.beans.entity.Users;
import com.qfedu.mall.beans.vo.RegisterVo;
import com.qfedu.mall.common.vo.*;
import com.qfedu.mall.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author dyw
 * @Date 2021年12月02日 9:20
 */

@RestController
@CrossOrigin
@RequestMapping("/product")
@Api(value = "提供用户信息信息相关接口",tags = "用户管理")
public class UserController {


    @Autowired
    private UserService userService;

    @Autowired
    private Kaptcha Kaptcha;

    /**
     * 获取用户资料
     */
    @GetMapping("/getUserInfo")
    @ApiOperation("根据用户id查询用户信息")
    @ApiImplicitParam(dataType = "Integer",name = "userId",value = "用户id",required = true)
    public ResultVO getUserInfo(Integer userId){
        System.out.println("userId:" + userId);
        ResultVO resultVO = userService.getUserInfo(userId);
        return resultVO;
    }

    @ApiOperation("用户登录接口")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "string",name="username",value="用户登录账号",required = true),
            @ApiImplicitParam(dataType = "string",name="password",value="用户登录密码",required = true)
    })

    @RequestMapping(value="/login", method= RequestMethod.POST)
    public ResultVO login(@RequestBody LoginVo loginVo){
//        LoginVo loginVo = JSON.parseObject(JSON.parseObject(param).getString("params"),LoginVo.class);
        System.out.println(loginVo);
        ResultVO resultVO = userService.login(loginVo.getUsername(), loginVo.getPassword());

        return resultVO;
    }



    @ApiOperation("用户注册接口")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "string",name="username",value="用户登录账号",required = true),
            @ApiImplicitParam(dataType = "string",name="password",value="用户登录密码",required = true)
    })

    @RequestMapping(value="/regist", method= RequestMethod.POST)
    public ResultVO regist(@RequestBody RegisterVo registerVo){

        String kaptcha = registerVo.getCaptcha_str();
        System.out.println("验证码：" + kaptcha);
        try {
            if (kaptcha.equals("6688")){
                System.out.println("验证码验证通过");
                ResultVO resultVO = userService.regist(registerVo.getUsername(),registerVo.getPassword(),registerVo.getNickName());
                return resultVO;
            }
        } catch (Exception e) {
            e.printStackTrace();

        }
        System.out.println("验证码验证失败");
        return new ResultVO(ResStatus.NO,"验证码错误",null);

    }

    @GetMapping("/getAllUsers")
    @ApiOperation("查询所有用户信息")
    public ResultVO1 getAllUsers(PageVo pageVo){
        return userService.getAllUsers(pageVo);
    }


    @PostMapping("/deleteUsers")
    @ApiOperation("根据ID删除用户接口")
    @ApiImplicitParam(dataType = "Integer",name = "userId", value = "用户id",required = true)
    public ResultVO deleteUsersById(@RequestBody UserId userId){
        System.out.println("+++++>userId:" + userId);
        return userService.delectUsersById(userId.getUserId());
    }


    @PostMapping("/addUser")
    @ApiOperation("用户添加接口")
    public ResultVO addUser(@RequestBody Users user){
        System.out.println("用户添加接口: user:" + user);
//        System.out.println("source:" + source);
        return userService.addUsers(user);
    }


    @ApiOperation("用户修改接口")
    @PostMapping("/updateuser")
    public ResultVO updateNum(@RequestBody Users user){
        System.out.println("修改用户：user：" + user);
        return userService.updateUser(user);
    }

    @GetMapping("/getAllUser")
    @ApiOperation("获取所有用户无分页")
    public ResultVO getAll(){

        ResultVO resultVO = userService.getall();
        return resultVO;
    }

}
